package com.yunyu.demo;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.yunyu.camera.mediacodec.CameraRecordState;
import com.yunyu.camera.opengl.LTGLRenderManager;
import com.yunyu.camera.opengl.LTRTCFilterManager;

import java.io.File;

public class CameraWindowActivity extends AppCompatActivity {
    private final String TAG = CameraWindowActivity.class.getSimpleName();

    private static final String CACHE_CONVERT = "/ffmpeg/outputConvert/";

    LTGLRenderManager ltglRenderManager;

    CameraActivityHelper cameraActivityHelper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.frag_camera);

        cameraActivityHelper = new CameraActivityHelper(this);

        testWindowRender();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (ltglRenderManager != null)
            ltglRenderManager.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (ltglRenderManager != null)
            ltglRenderManager.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ltglRenderManager != null)
            ltglRenderManager.onDestroy();
    }

    private void testWindowRender(){
        ConstraintLayout frameLayout = findViewById(R.id.glCameraBox);
        TextView glTittleTV = findViewById(R.id.glTittle);
        glTittleTV.setText("窗口渲染");
        ltglRenderManager = new LTGLRenderManager(getApplicationContext());
        ltglRenderManager.setCallback(new LTGLRenderManager.LTGLRenderManagerCallback() {
            @Override
            public void onRenderLayout(FrameLayout renderLayout) {
                int screenWidth = getResources().getDisplayMetrics().widthPixels;
                int screenHeight = getResources().getDisplayMetrics().heightPixels;
                int width = screenWidth;
                int height = (int)(width * 16f / 9f);
                if (height > screenHeight){
                    height = screenHeight;
                    width = (int)(height * 9f / 16f);
                }
                int x = (screenWidth - width) / 2;
                int y = (screenHeight - height) / 2;
                ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(width, height);
                layoutParams.setMargins(x,y,x+width,y+height);
                frameLayout.addView(renderLayout,0, layoutParams);
            }

            @Override
            public void onFilterManager(LTRTCFilterManager filterManager) {
                cameraActivityHelper.testControl(filterManager);
            }
        });
        ltglRenderManager.onCreate();

        //切换后置摄像头
        CheckBox backCameraCB = findViewById(R.id.glBackCamera);
        backCameraCB.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                ltglRenderManager.flipCamera(LTGLRenderManager.CameraDirection.BACK);
            }
            else {
                ltglRenderManager.flipCamera(LTGLRenderManager.CameraDirection.FRONT);
            }
        });

        //视频录制按钮
        CheckBox videoRecordCb = findViewById(R.id.glVideoRecord);
        videoRecordCb.setVisibility(View.VISIBLE);
        videoRecordCb.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked){
                buttonView.post(new Runnable() {
                    @Override
                    public void run() {
                        testStartRecord();
                    }
                });
                Toast.makeText(this,"开始录制",Toast.LENGTH_SHORT).show();
            }
            else {
                testStopRecord();
                Toast.makeText(this,"停止录制",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 开启录制 gop推荐2s或更高 码率自定义:单位kbps 0位内部自动计算合适码率
     */
    public void testStartRecord(){
        String dstVideoName = "otherType.mp4";
        File dstVideo = getCacheFile(CACHE_CONVERT, dstVideoName);
        ltglRenderManager.prepareMovieRecord(dstVideo.getAbsolutePath(),
                720, 1280, 30, 30 * 2, 1500 * 1024,
                true, new CameraRecordState() {
                    @Override
                    public void onRecordFailure() {
                        Log.d(TAG, "onRecordFailure: ");
                    }

                    @Override
                    public void onStart() {
                        Log.d(TAG, "onStart: ");
                    }

                    @Override
                    public void onStop() {
                        Log.d(TAG, "onStop: ");
                    }
                });
        ltglRenderManager.startRecord();
    }

    /**
     * 停止录制
     */
    public void testStopRecord(){
        ltglRenderManager.stopRecord();
    }

    /**
     * 截屏
     */
    public void testCapture(){
        Bitmap bitmap = ltglRenderManager.capture();
        if (bitmap !=null)
            Log.d(TAG, String.format("testCapture: %dx%d",bitmap.getWidth(),bitmap.getHeight()));
    }

    /**
     * 设置窗口显示比例
     */
    public void testSetRatio(){
        //9/16f 3/4f 1/1f
        ltglRenderManager.setRatio(9/16f);
    }

    private File getCacheFile(String dir, String name) {
        File tmpDir = new File(getApplicationContext().getCacheDir() + dir);
        if (!tmpDir.exists())
            tmpDir.mkdirs();
        File file = new File(tmpDir.getAbsolutePath(), name);
        return file;
    }
}
